<?php

/**
 * kcDoctrineRecord
 *
 * @package    flexiwik
 * @subpackage lib.model
 * @author     Jonathan Olger Nieto Lajo <jonathan_nieto@hotmail.com>
 */
abstract class kcDoctrineRecord extends sfDoctrineRecord
{
  const 
    SI       = '1',
    NO       = '0',
    ACTIVO   = '1',
    INACTIVO = '0';
    
  public $_values;
  
  public function isColumnModified($col)
  {
    return in_array($col, $this->_modified);
  }
  
  public function unsetModifieds($cols = array(), $set_null = false)
  {
  	if ($set_null)
  	{
  	  foreach($cols as $col)
  	  {
  	    call_user_func(array($this, 'set'.sfInflector::camelize($col)), null);
  	  }
  	}
  	
    $flipModified = array_flip($this->_modified);
    
  	foreach($cols as $col)
  	{
  	  if (array_key_exists($col, $flipModified))
  	  {
  	    unset($this->_modified[$flipModified[$col]]);
  	  }
  	}
  }
  
  public function unsetFields($fields = array())
  {
  	foreach($fields as $field)
  	{
  	  $this->__unset($field);
  	}
  }
  
  public function __call($method, $arguments)
  {
    try
    {
      if (in_array($verb = substr($method, 0, 3), array('set', 'get')))
      {
        $name = substr($method, 3);
        $entityName = sfInflector::underscore($name);
        
        if (array_key_exists($entityName, $this->_values))
        {
          return $this->_values[$entityName];
        }
        
        return parent::__call($method, $arguments);
      }
      elseif (substr($method, 0, 3) == 'add')
      {
        $name = substr($method, 3);
        $collectionName = $name.'s';
        
        return $this->$collectionName->add($arguments[0]);
      }
      else
      {
        return parent::__call($method, $arguments);
      }
    }
    catch(Exception $e)
    {
      return parent::__call($method, $arguments);
    }
  }
  
  protected function executeIfExists()
  {
    $args   = func_get_args();
    $method = array_shift($args);
     
    if (method_exists($this, $method))
    {
      return call_user_func_array(array($this, $method), $args);
    }
  }
  
  
  public function unlink($alias, $ids = array())
  {
    $ids = (array) $ids;

    $q = new Doctrine_Query();

    $rel = $this->getTable()->getRelation($alias);

    if ($rel instanceof Doctrine_Relation_Nest)
    {
      $q->delete()
        ->from($rel->getAssociationTable()->getComponentName())
        ->where($rel->getLocal() . ' = ?', array_values($this->identifier()))
        ->orWhere($rel->getForeign() . ' = ?', array_values($this->identifier()));

      if (count($ids) > 0)
      {
        $q->whereIn($rel->getForeign(), $ids);
        $q->orWhereIn($rel->getLocal(), $ids);
      }

      $q->execute();
    }
    if ($rel instanceof Doctrine_Relation_Association)
    {
      $q->delete()
        ->from($rel->getAssociationTable()->getComponentName())
        ->where($rel->getLocal() . ' = ?', array_values($this->identifier()));

      if (count($ids) > 0)
      {
        $q->whereIn($rel->getForeign(), $ids);
      }

      $q->execute();

    }
    else if ($rel instanceof Doctrine_Relation_ForeignKey)
    {
      $q->update($rel->getTable()->getComponentName())
        ->set($rel->getForeign(), '?', array(null))
        ->addWhere($rel->getForeign() . ' = ?', array_values($this->identifier()));

      if (count($ids) > 0)
      {
        $q->whereIn($rel->getTable()->getIdentifier(), $ids);
      }

      $q->execute();
    }
    
    if (isset($this->_references[$alias]))
    {
      foreach ($this->_references[$alias] as $k => $record)
      {
        if (in_array(current($record->identifier()), $ids))
        {
          $this->_references[$alias]->remove($k);
        }
      }
      $this->_references[$alias]->takeSnapshot();
    }
    
    return $this;
  }
}
